package com.atguigu.staservice.service.impl;

import com.atguigu.commonutils.R;
import com.atguigu.staservice.client.UcenterClient;
import com.atguigu.staservice.entity.StatisticsDaily;
import com.atguigu.staservice.mapper.StatisticsDailyMapper;
import com.atguigu.staservice.service.StatisticsDailyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-12-27
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {
    @Autowired
    private UcenterClient ucenterClient;

    @Override
    public void registerCount(String day) {
        //添加记录之前删除表里相同日期的数据,同一日期表里只允许存在一条
        QueryWrapper<StatisticsDaily> wrapper=new QueryWrapper<>();
        wrapper.eq("date_calculated",day);
        baseMapper.delete(wrapper);


            //远程调用得到某一天注册认数
        R r = ucenterClient.countRegister(day);
        Integer countPersons = (Integer) r.getData().get("countRegister");

        //把获取到的数据添加到数据库
        StatisticsDaily statisticsDaily = new StatisticsDaily();
        statisticsDaily.setRegisterNum(countPersons);//统计认数
        statisticsDaily.setDateCalculated(day);//统计日期
        statisticsDaily.setVideoViewNum(RandomUtils.nextInt(100,500));
        statisticsDaily.setCourseNum(RandomUtils.nextInt(100,500));
        statisticsDaily.setLoginNum(RandomUtils.nextInt(100,500));

        baseMapper.insert(statisticsDaily);

    }

    @Override
    public Map<String, Object> getShowData(String type, String begin, String end) {
        QueryWrapper<StatisticsDaily> wrapper=new QueryWrapper<>();
        //条件查询  日期between
        wrapper.between("date_calculated",begin,end);
        wrapper.select("date_calculated",type);
        List<StatisticsDaily> list = baseMapper.selectList(wrapper);

        //返回两部分数据，日期，日期对应的数量
        //前端要求json结构,du对应后端的list集合
        List<String> date_calculated=new ArrayList<>();
        List<Integer> numDataList=new ArrayList<>();

        //遍历查询出的数据
        for (int i = 0; i < list.size(); i++) {
            StatisticsDaily daily = list.get(i);
            //封装日期
            date_calculated.add(daily.getDateCalculated());
            //封装数量
            switch (type){
                case "login_num":
                    numDataList.add(daily.getLoginNum());
                    break;
                case "register_num":
                    numDataList.add(daily.getRegisterNum());
                    break;
                case "video_view_num":
                    numDataList.add(daily.getVideoViewNum());
                    break;
                case "course_num":
                    numDataList.add(daily.getCourseNum());
                    break;
                default:
                    break;
            }
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("date_calculated",date_calculated);
        map.put("numDataList",numDataList);
        return map;
    }
}
